﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class DatHangDAO : DataProvider
    {
        public ArrayList GetDSDonDatHang()
        {
            connect();
            string sql = "select * from DonDatHang";
            adapter = new SqlDataAdapter(sql, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ArrayList arr = ConvertDataSetToArrayList(dataset);
            disconnect();
            return arr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            DatHangDTO dhDTO = new DatHangDTO();
            dhDTO.MADH = dt.Rows[i][1].ToString();
            dhDTO.NGAYLAP = DateTime.Parse(dt.Rows[i][2].ToString());
            dhDTO.NGUOILAP = int.Parse(dt.Rows[i][3].ToString());
            dhDTO.NGAYGIAO = DateTime.Parse(dt.Rows[i][4].ToString());

            return (object)dhDTO;
        }

        public void ThemDonDatHang(DatHangDTO dh)
        {
            connect();
            string sql = "INSERT INTO DonDatHang VALUES('" + dh.MADH + "','" + dh.NGAYLAP + "', 2 ,'" + dh.NGAYGIAO + "')";
            executeNonQuery(sql);
            disconnect();
        }

        public int GetDonHangID(string MaDH)
        {
            connect();
            string sql = "SELECT dat_hang_id FROM DonDatHang WHERE ma_dat_hang = '"+ MaDH +"'";
            SqlCommand cmd = new SqlCommand(sql, connection);
            SqlDataReader reader = cmd.ExecuteReader();
            int res = 0;
            while (reader.Read())
                res = reader.GetInt32(0);
            disconnect();
            return res;
        }
    }
}
